

import { createRouter, createWebHistory} from 'vue-router'
import LocalCache from '@/utils/cache'

import dialogVisible from "@/utils/isdialogVisible"


const routes = [
  {
    path: '/',
    redirect: '/main/0'
  },
  {
    path: '/details',
    redirect: '/details/0'
  },
  // {
  //   path: '/login',
  //   name:'login',
  //   component:()=>import("@/view/login/src")
  // },
  {
    path: '/main',
    name: 'main',
    component: () => import('../view/main/main.vue'),
    children:[
      {
        path:'/main/0',
        component:()=> import("@/view/main/cpn/main-focus")
      },
      {
        path:'/main/1',
        component:()=> import("@/view/main/cpn/main-recommend.vue")
      },
      {
        path:'/main/2',
        component:()=> import("@/view/main/cpn/main-video.vue")
      }
    ]
  },
  {
    path :'/study',
    name:'study',
    component:()=>import("@/view/study/src")
  },
  {
    path :'/answer',
    name:'answer',
    component:()=>import("@/view/answer/src"),
    children:[
      {
        path:'/answer/:id',
        component:()=> import("@/view/answer/src/cpn/index.vue")
      }
    ]
  },
  {
    path :'/find',
    name:'find',
    component:()=>import("@/view/find/src")
  },
  {
    path :'/pal',
    name:'pal',
    component:()=>import("@/view/pal/src/index.vue"),
  },
  {
    path:'/wangeditor',
    name:'wangeditor',
    component:()=>import("@/view/wangeditor/wangeditor"),
    children:[
      { 
        path:'/wangeditor/:issue',
        component:()=>import("@/view/wangeditor/wangeditor")
      },
    ]
  },
  {
    path:'/question',
    name:"question",
    component:()=>import("@/view/question/src")
  },
  {
    path:'/details',
    name:"details",
    component:()=>import("@/view/info"),
    children:[
      {
        path:'/details/:id',
        component:()=>import("@/view/info/src/activity")
      },
      {
        path:'/details/wallet',
        component:()=>import("@/view/info/src/wallet")
      },
      {
        path:'/details/file',
        component:()=>import("@/view/info/src/file")
      },

    ]
  },




  
  // {
  //   path: '/:pathMatch(.*)*',
  //   name: 'not-found',
  //   component: () => import('../view/not-found/not-found.vue')
  // }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
// router.beforeEach((to)=>{
//   if(to !== '/login') {
//     const token = LocalCache.getCache('token')
//     if (!token ) {
//       return '/login'
//     } 
//   }
// })
//判断一下不在login页面上的所有界面都携带的有没有token如果没有那就跳转到login
router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = LocalCache.getCache('token')
   if(!token) {
    dialogVisible.value = true
   }else {
    dialogVisible.value = false
   }
  }
})  

export default router
